﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace Trackr.Core
{
	/// <summary>
	/// Helper class to perform windows specific stuff, registry edit etc
	/// </summary>
	public static class RegistryUtility
	{
		private const string RUN_LOCATION = @"Software\Microsoft\Windows\CurrentVersion\Run";
		#region registry write and get
		public static void SetRegistry(string key, string valueName)
		{
			var regKey = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
			regKey.SetValue(key, valueName);
		}
		public static void UnSetRegistry(string valueName)
		{
			var regKey = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
			if (regKey.GetValue(valueName) != null)
				regKey.DeleteValue(valueName);
		}
		public static T GetRegistryValue<T>(string valueName)
		{
			var regKey = Registry.CurrentUser.OpenSubKey(RUN_LOCATION);
			if (regKey == null)
				return default(T);
			return (T)regKey.GetValue(valueName);
		}
		public static bool AutoStart(bool autoStart, string runningAssemblyName, string Identifier = "Trackr Timetracking")
		{
			//Set or unset registry
			if (autoStart)
				RegistryUtility.SetRegistry(Identifier, runningAssemblyName);
			else
				RegistryUtility.UnSetRegistry(Identifier);
			
			//get value from registry if it works
			string value = RegistryUtility.GetRegistryValue<string>(Identifier);
			if (value == null)
				return false;
			return (value == runningAssemblyName);
		}
		#endregion
	}
}
